<?php

class Model_user extends CI_Model{
	function __construct(){
        parent::__construct();
	}
	
	function register($type='user'){
		$data = array(
		   'username' => $this->input->post("username",true) ,
		   'password' => md5($this->input->post("password",true)) ,
		   'name' => $this->input->post("name",true) ,
		   'email' => $this->input->post("email",true),
		   'phone' => $this->input->post("phone",true),
		   'address' => $this->input->post("address",true),
		   'last_logged_in' => date("Y-m-d H:i:s"),
		   'is_active' => 'Y',
		   'type' => $type,
		   'member_since' => date("Y-m-d H:i:s")
		);
		
		$this->db->insert('user', $data); 
	}
	function checkunique($field,$str){
		$this->db->select("*");
		$this->db->from("user");
		$this->db->where($field,$str);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return false;
		}else{
			return true;
		}
	}
	
	function forgot_password(){ // save time and generate secret key, then return the secret key
	}
	
	function checkemail($str){
		$this->db->select("*");
		$this->db->from("user");
		$this->db->where("email",$str);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	
	function getDetail($user_id){
		$this->db->select("*");
		$this->db->from("user");
		$this->db->where("user_id",$user_id);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->row_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	
	function getKarList(){
		$this->db->select("*");	
		$this->db->from('user');
		$this->db->where("type","user");
		$this->db->where("is_active","Y");
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	function getAdminList($params=array()){
		$this->db->select("*");
		$this->db->from("user");
		$this->db->where("type","user");
		$this->db->where("is_active","Y");
		//buat search nih..
		if(isset($params['keyword']) ){
			$keyword_ex = explode(" ",$params['keyword']);
			$where = "";
			foreach($keyword_ex as $keyword){
				if($where == ""){
					$where = "(";
				}else{
					$where .= " or ";
				}
				$where .= " username like '%".$keyword."%'";
				$where .= " or name like '%".$keyword."%'";
				$where .= " or email like '%".$keyword."%'";
				$where .= " or phone like '%".$keyword."%'";
			}
			if($where != "") $where .= ")";
			$this->db->where($where, NULL, FALSE);
		}
		
		if(isset($params['order_by']) ) $this->db->order_by($params['order_by']);
		if(isset($params['limit_1'])  && isset($params['limit_2']) ) $this->db->limit($params['limit_1'],$params['limit_2']);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function getUserList($params=array()){
		$this->db->select("*");
		$this->db->from("user");
		$this->db->where("type","user");
		$this->db->where("is_active","Y");
		//buat search nih..
		if(isset($params['keyword']) ){
			$keyword_ex = explode(" ",$params['keyword']);
			$where = "";
			foreach($keyword_ex as $keyword){
				if($where == ""){
					$where = "(";
				}else{
					$where .= " or ";
				}
				$where .= " username like '%".$keyword."%'";
				$where .= " or name like '%".$keyword."%'";
				$where .= " or email like '%".$keyword."%'";
				$where .= " or phone like '%".$keyword."%'";
			}
			if($where != "") $where .= ")";
			$this->db->where($where, NULL, FALSE);
		}
		
		if(isset($params['order_by']) ) $this->db->order_by($params['order_by']);
		if(isset($params['limit_1'])  && isset($params['limit_2']) ) $this->db->limit($params['limit_1'],$params['limit_2']);
		$query = $this->db->get();
		if($query->num_rows() > 0){
			$hasil = $query->result_array();
			return $hasil;
		}else{
			return array();
		}
	}
	
	function delete($user_id){
		$data = array(
		   'is_active' => 'N'
		);
		
		$this->db->where('user_id', $user_id);
		$this->db->update('user', $data); 
	}
	function changepassword($newpassword){
		$password = md5($newpassword);
		$data = array(
               'password' => $password
            );

		$this->db->where('user_id', $this->session->userdata("user_id"));
		$this->db->update('user', $data);
	}
	
	function changepasswordKar($id,$newpassword){
		$password = md5($newpassword);
		$data = array(
               'password' => $password
            );

		$this->db->where('user_id', $id);
		$this->db->update('user', $data);
	}
	function checkpassword($password){
		$this->db->select("*");
		$this->db->from("user");
		$this->db->where("user_id",$this->session->userdata("user_id"));
		$this->db->where("password",md5($password));
		$query = $this->db->get();
		if($query->num_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
	
	function edit_profile(){
		$data = array(
		   'name' => $this->input->post("name",true),
		   'email' => $this->input->post("email",true),
		   'phone' => $this->input->post("phone",true),
		   'address' => $this->input->post("address",true)
		);
		$this->db->where("user_id",$this->session->userdata("user_id"));
		$this->db->update('user', $data); 
	}
	
	function edit_kar($id){
		$data = array(
		   'name' => $this->input->post("name",true),
		   'email' => $this->input->post("email",true),
		   'phone' => $this->input->post("phone",true),
		   'address' => $this->input->post("address",true)
		);
		$this->db->where("user_id",$id);
		$this->db->update('user', $data); 
	}
}